//
//  ICChatMessageTopView.m
//  ICome
//
//  Created by zhangrongwu on 2017/9/11.
//  Copyright © 2017年 iCom. All rights reserved.
//

#import "ICChatMessageTopView.h"
#import "ICHeaderFile.h"
@interface ICChatMessageTopView()
@property (nonatomic, strong)UIView *bgView;
@property (nonatomic, strong)UIView *line;
@property (nonatomic, strong)UIView *keyValueView;
@property (nonatomic, strong)UIButton *delete;
@property (nonatomic, strong)NSMutableArray *content;

@end
@implementation ICChatMessageTopView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self addSubview:self.bgView];
        [self.bgView addSubview:self.titleLabel];
        [self.bgView addSubview:self.line];
        [self.bgView addSubview:self.keyValueView];
        [self addSubview:self.delete];
        [self addSubview:self.end];
        WEAKSELF
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
            if (![ICTools stringEmpty:weakSelf.url]) {
                if([weakSelf.delegate respondsToSelector:@selector(clickTopMessage:)]) {
                    [weakSelf.delegate clickTopMessage:self.url];
                }
            }
        }];
        [self addGestureRecognizer:tap];
    }
    return self;
}

- (void)deleteMessage {
    if ([self.delegate respondsToSelector:@selector(deleteTopMessage)]) {
        [self.delegate deleteTopMessage];
    }
}

-(void)setData:(id)data {
    self.url = data[@"url"] == nil ? @"":data[@"url"];
    self.content = data[@"content"];
    [self layoutSubview];
}

- (void)layoutSubview {
    
    [self.bgView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top).offset(15);
        make.left.equalTo(self.mas_left).offset(15);
        make.right.equalTo(self.mas_right).offset(-15);
        make.bottom.equalTo(self.keyValueView.mas_bottom).offset(10);
    }];
    
    [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bgView.mas_top).offset(10);
        make.left.equalTo(self.bgView.mas_left).offset(10);
        make.right.equalTo(self.bgView.mas_right).offset(-10);
    }];
    
    [self.delete mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.bgView.mas_right);
        make.centerY.equalTo(self.bgView.mas_top);
        make.width.mas_equalTo(30);
        make.height.mas_equalTo(30);
    }];
    
    [self.line mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(5);
        make.left.equalTo(self.bgView.mas_left).offset(10);
        make.right.equalTo(self.bgView.mas_right).offset(-10);
        make.height.mas_equalTo(0.5);
    }];
    
    [self drawLineOfDashByCAShapeLayer:self.line lineLength:5 lineSpacing:3 lineColor:ICRGB(0xE0E0E0) lineDirection:YES];
   
    NSInteger subCount = 2;
    if (self.content.count <= 2) {
        subCount = self.content.count;
    }
    NSMutableArray *list = [[NSMutableArray alloc] init];
    for (int i = 0; i < subCount; i++) {
        UILabel *label = [[UILabel alloc] init];
        NSDictionary *keyValue = self.content[i];
        label.text = [NSString stringWithFormat:@"%@ ：%@", keyValue[@"key"], keyValue[@"value"]];
        label.font = [UIFont systemFontOfSize:14];
        label.textColor = ICRGB(0x969696);
        [list addObject:label];
        [self.keyValueView addSubview:label];
    }
    
    [list mas_distributeViewsAlongAxis:MASAxisTypeVertical withFixedSpacing:5 leadSpacing:1 tailSpacing:1];
    [list mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
    }];
    
    [self.keyValueView mas_remakeConstraints:^(MASConstraintMaker *make) {
        UILabel *label = (UILabel *)[list lastObject];
        make.top.equalTo(self.line.mas_bottom).offset(10);
        make.left.equalTo(self.bgView.mas_left).offset(10);
        make.right.equalTo(self.bgView.mas_right).offset(-10);
        make.bottom.equalTo(label.mas_bottom);
    }];
    
    [self.end mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bgView.mas_bottom);
        make.bottom.equalTo(self.mas_bottom);
    }];
}

-(NSMutableArray *)content {
    if (!_content) {
        _content = [[NSMutableArray alloc] init];
    }
    return _content;
}

-(UIButton *)delete {
    if (!_delete) {
        _delete = [[UIButton alloc] init];
        [_delete setImage:[UIImage imageNamedFromiComeSDKBundle:@"chat_top_message_delete"] forState:UIControlStateNormal];
        [_delete addTarget:self action:@selector(deleteMessage) forControlEvents:UIControlEventTouchUpInside];
    }
    return _delete;
}

-(UILabel *)titleLabel {
    if(!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font = [UIFont systemFontOfSize:16];
        _titleLabel.numberOfLines = 0;
        _titleLabel.textColor = ICRGB(0x222222);
        _titleLabel.textAlignment = NSTextAlignmentLeft;
        _titleLabel.userInteractionEnabled = YES;
    }
    return _titleLabel;
}

-(UIView *)bgView {
    if (!_bgView) {
        _bgView = [[UIView alloc] init];
        _bgView.backgroundColor = ICRGB(0xf2f2f2);
        _bgView.layer.cornerRadius = 5;
        _bgView.layer.masksToBounds = YES;
    }
    return _bgView;
}

-(UIView *)line {
    if (!_line) {
        _line = [[UIView alloc] init];
        _line.backgroundColor = ICRGB(0xE0E0E0);
    }
    return _line;
}

-(UIView *)keyValueView {
    if (!_keyValueView) {
        _keyValueView = [[UIView alloc] init];
    }
    return _keyValueView;
}

-(UIView *)end {
    if (!_end) {
        _end = [[UIView alloc] init];
    }
    return _end;
}

- (void)drawLineOfDashByCAShapeLayer:(UIView *)lineView lineLength:(int)lineLength lineSpacing:(int)lineSpacing lineColor:(UIColor *)lineColor lineDirection:(BOOL)isHorizonal {
    
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    [shapeLayer setBounds:lineView.bounds];
    if (isHorizonal) {
        [shapeLayer setPosition:CGPointMake(CGRectGetWidth(lineView.frame) / 2, CGRectGetHeight(lineView.frame))];
    } else{
        [shapeLayer setPosition:CGPointMake(CGRectGetWidth(lineView.frame) / 2, CGRectGetHeight(lineView.frame)/2)];
    }
    [shapeLayer setFillColor:[UIColor clearColor].CGColor];
    //  设置虚线颜色为blackColor
    [shapeLayer setStrokeColor:lineColor.CGColor];
    //  设置虚线宽度
    if (isHorizonal) {
        [shapeLayer setLineWidth:CGRectGetHeight(lineView.frame)];
    } else {
        [shapeLayer setLineWidth:CGRectGetWidth(lineView.frame)];
    }
    [shapeLayer setLineJoin:kCALineJoinRound];
    //  设置线宽，线间距
    [shapeLayer setLineDashPattern:[NSArray arrayWithObjects:[NSNumber numberWithInt:lineLength], [NSNumber numberWithInt:lineSpacing], nil]];
    //  设置路径
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, 0, 0);
    if (isHorizonal) {
        CGPathAddLineToPoint(path, NULL,CGRectGetWidth(lineView.frame), 0);
    } else {
        CGPathAddLineToPoint(path, NULL, 0, CGRectGetHeight(lineView.frame));
    }
    
    [shapeLayer setPath:path];
    CGPathRelease(path);
    //  把绘制好的虚线添加上来
    [lineView.layer addSublayer:shapeLayer];
}

@end
